<template>
  <div>
    <el-dialog
      title="新增客户"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="clientAddData" :model="clientAddData" label-width="80px">
        <el-form-item label="经纪">
          <el-select v-model="clientAddData.agent" placeholder="请选择活动区域">
            <el-option
              v-for="item in addAhentData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="clientAddData.name" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="clientAddData.email" />
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="clientAddData.phone" />
        </el-form-item>
        <el-form-item label="微信">
          <el-input v-model="clientAddData.wechat" />
        </el-form-item>
        <el-form-item type="textarea" label="描述" prop="desc">
          <el-input v-model="clientAddData.desc" />
        </el-form-item>
        <el-form-item label="封面">
          <ImageUpload ref="ImageUploadImg" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clientResetFn">重置</el-button>
        <el-button type="primary" @click="clientAddFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getClientAdd, getClientPut } from "@/api/client";
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    addAhentData: {
      type: Array,
      default: () => {
        return [];
      },
    },
    editorData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      cliectAdd: "",
      clientAddData: {
        agent: "", // 属于哪个经纪的 id 值
        name: "", // 名称
        email: "", // 电子邮箱，比如 example@qq.com
        phone: "", // 电话
        wechat: "", // 微信
        desc: "", // 描述
        image: "", // 图片
      },
      // 校验表单
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        desc: [{ required: true, message: "请输入描述", trigger: "blur" }],
      },
      resetImg: "", //重置img
    };
  },
  methods: {
    // 渲染经纪
    delImgFn(){},
    // 重置按钮
    clientResetFn() {
    },
    // 编辑
    editorDataFn() {
      this.clientAddData = this.editorData;
      // 回显图片
      this.resetImg = this.clientAddData.image;
      const upload = this.$refs.ImageUploadImg;
      upload.fileList.push({ name: "a,jpeg", url: this.clientAddData.image });
    },
    getimg() {
      const upload = this.$refs.ImageUploadImg;
      this.clientAddData.image = upload.fileList[0].url;
    },
    // 确认新增
    async clientAddFn() {
      this.$refs.clientAddData.validate();
      if (!this.clientAddData.id) {
        this.getimg();
        await getClientAdd(this.clientAddData);
      }else if (this.clientAddData.id) {
        await getClientPut(this.clientAddData)
      }
      this.$emit("clientVisibleFn");
      this.$refs.clientAddData.resetFields();
      this.clientAddData = {
        agent: "", // 属于哪个经纪的 id 值
        name: "", // 名称
        email: "", // 电子邮箱，比如 example@qq.com
        phone: "", // 电话
        wechat: "", // 微信
        desc: "", // 描述
        image: "", // 图片
      };
    },
    // X按钮
    handleClose() {
      this.$emit("handleCloseFn");
      const upload = this.$refs.ImageUploadImg;
      upload.fileList = [];
      this.resetImg = "";
      this.clientAddData= {
        agent: "", // 属于哪个经纪的 id 值
        name: "", // 名称
        email: "", // 电子邮箱，比如 example@qq.com
        phone: "", // 电话
        wechat: "", // 微信
        desc: "", // 描述
        image: "", // 图片
      }
    },
  },
};
</script>

<style>
</style>
